#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Introduction to Programming in Java - An Interdisciplinary Approach

All book programs ported to Python by
Scott C. King (revisionx at {gee}ma1l <dot> c0m).

All program examples pass Pylint with a perfect 10.00 score.
Pylint:  http://www.pylint.org/


Creative Exercise 1.4.26 - Hadamard matrix

Syntax:
python hadamardmatrix.py 8

'''


import sys


def main():
    ''' Print an N-by-N Hadamard matrix '''

    matrix = [['T']]
    dimension = 1

    while dimension < int(sys.argv[1]):
        next_matrix = []

        # Build the top 2 quadrants row by row
        for quadrant_row in range(len(matrix)):
            next_matrix += [matrix[quadrant_row] + matrix[quadrant_row]]

        # Build the bottom 2 quadrants row by row
        for quadrant_row in range(len(matrix)):

            # Invert items in bottom right quadrant row by row
            inverted_quadrant = matrix[quadrant_row][:]
            for row_val in range(len(inverted_quadrant)):
                if inverted_quadrant[row_val] == 'T':
                    inverted_quadrant[row_val] = 'F'
                else:
                    inverted_quadrant[row_val] = 'T'
            next_matrix += [matrix[quadrant_row] + inverted_quadrant]

        matrix = next_matrix
        dimension *= 2

    for row in matrix:
        for col in row:
            print col,
        print


if __name__ == '__main__':
    main()
